package com.itbegin.init;

import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;


@Configuration
@ComponentScan(basePackages="com.itbegin.*")
@EnableWebMvc
@EnableAsync
public class MyWebConfig extends WebMvcConfigurerAdapter{
	
	@Override
	public void configureDefaultServletHandling(
			DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		super.addResourceHandlers(registry);
		registry.addResourceHandler("/js/**").addResourceLocations("/js/");
		registry.addResourceHandler("/css/**").addResourceLocations("/css/");
		registry.addResourceHandler("/images/**").addResourceLocations("/images/");
	}
	
	@Bean
	public FreeMarkerConfigurer freeMarkerConfigurer(){
		FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
		configurer.setTemplateLoaderPath("/WEB-INF/template/");
		configurer.setDefaultEncoding("UTF-8");
		return configurer;
	}
	
	@Bean
	public FreeMarkerViewResolver viewResolver() {
		FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
		viewResolver.setPrefix("");
		viewResolver.setSuffix(".ftl");
		viewResolver.setContentType("text/html;charset=UTF-8");
		return viewResolver;
	}
	
	@Override
	public void configureMessageConverters(
			List<HttpMessageConverter<?>> converters) {
		converters.add(stringHttpMessageConverter());
		converters.add(jsonHttpMessageConverter());
		 
	}

	@Bean 
	MappingJackson2HttpMessageConverter jsonHttpMessageConverter(){
		Jackson2ObjectMapperBuilder builder = new
				  Jackson2ObjectMapperBuilder() .indentOutput(true) .dateFormat(new
				  SimpleDateFormat("yyyy-MM-dd")) ; 
		MappingJackson2HttpMessageConverter
			jmConverter = new MappingJackson2HttpMessageConverter(builder.build());
		jmConverter.setSupportedMediaTypes
				  (Arrays.asList(MediaType.APPLICATION_JSON_UTF8));
		return jmConverter;
	}
	
	@Bean
	public StringHttpMessageConverter stringHttpMessageConverter() {
		StringHttpMessageConverter strConverter = new StringHttpMessageConverter(
				Charset.forName("UTF-8"));
		strConverter.setSupportedMediaTypes(Arrays.asList(
				new MediaType("text","html", Charset.forName("UTF-8")), 
				new MediaType("text","plain", Charset.forName("UTF-8"))));
		strConverter.setWriteAcceptCharset(false);
		return strConverter;
	}
}
